package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.CountrySiftResultBean.CountrySiftBean;

import java.util.ArrayList;

public class CountrySiftLeftAdapter extends BaseExpandableListAdapter {
    ArrayList<CountrySiftBean> datas;
    Context context;
    LayoutInflater inflater;

    public CountrySiftLeftAdapter(ArrayList<CountrySiftBean> datas, Context context) {
        this.datas = datas;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return datas != null ? datas.size() : 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return (datas != null && datas.get(groupPosition) != null && datas.get(groupPosition).getValue() != null) ? datas.get(groupPosition).getValue().size() : 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return datas.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return datas.get(groupPosition).getValue().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        Holder holder = new Holder();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.country_sift_left_group_item, null);
        }
        holder.name = (TextView) convertView.findViewById(R.id.sift_group_name);
        if (groupPosition < datas.size()) {
            holder.name.setText(datas.get(groupPosition).getZu().toUpperCase());
        }
        if (convertView.getTag() == null) {
            convertView.setTag(holder);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                             ViewGroup parent) {
        Holder holder = new Holder();
        try {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.country_sift_left_child_item, null);
            }
            holder.name = (TextView) convertView.findViewById(R.id.sift_child_name);
            if (groupPosition < datas.size() && childPosition < datas.get(groupPosition).getValue().size()) {
                holder.name.setText(datas.get(groupPosition).getValue().get(childPosition).getId());
            }
            if (convertView.getTag() == null) {
                convertView.setTag(holder);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class Holder {
        TextView name;
    }
}
